<template>
	<el-form :model="formData" :rules="formRules" ref="formRef" label-width="100px" label-position="left">
		<el-form-item label="用户组名称：" prop="name">
			<el-input v-model.trim="formData.name" @keyup.enter.native="save('formRef')"></el-input>
		</el-form-item>
		<el-form-item label="连续新增：" style="margin-bottom: 10px;" v-if="!tableRowId">
			<el-checkbox v-model="continueAdd"></el-checkbox>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" size="small" @click="save('formRef')">保存</el-button>
			<el-button type="danger" size="small" @click="close">关闭</el-button>
		</el-form-item>
	</el-form>
</template>
<script>
export default {
	data() {
		return {
			continueAdd: false,			//是否连续新增
			formData: {},		//表格数据
			formRules: {
				name: [{required: true, message: '名称不能为空', triggle: 'blur'}]
			},
			tableRowId: this.$store.state.userGroupStore.tableRowId
		}
	},
	computed: {
		systemId() {
			return this.$store.state.systemId;
		}
	},
	methods: {
		getUpdateData() {			//获取修改用户组的数据
			this.$http.get('userGroup/selectObjectById?id=' + this.tableRowId).then(res => {
				if(res.data.code == 20001) {
					if(res.data.data) {
						this.formData = res.data.data;
					}
				}
			});
		},
		save(formName) {			//保存
			this.$refs[formName].validate((valid) => {
                if (valid) {
                	let data = {
                		name: this.formData['name'],
                		systemId: this.systemId
                	}
                	if(this.formData['id']) {
                		//修改
                		data['id'] = this.formData['id'];
                		this.$http({
	                        url: 'userGroup/insertOrUpdateUserGroup',
	                        method: 'post',
	                        params: data
	                    }).then(res => {
	                    	if(res.data.code == 20001) {
                				this.$message({message: res.data.data || '修改成功', type: 'success', duration: 1500});
                				setTimeout(() => {
                					this.$store.dispatch('userGroupStore/getTableData');		//刷新表格数据
                					this.close();
                				}, 1000);
                			}
	                    });
                	} else {
                		//新增
                		this.$http({
                			url: 'userGroup/insertOrUpdateUserGroup',
                        	method: 'post',
                        	params: data
                		}).then(res => {
                			if(res.data.code == 20001) {
                				this.$message({message: res.data.data || '新增成功', type: 'success', duration: 1500});
                				setTimeout(() => {
                					this.$store.dispatch('userGroupStore/getTableData');		//刷新表格数据
                					this.continueAdd ? this.$refs[formName].resetFields() : this.close();
                				}, 1000);
                			}
                		});
                	}
                }
            });
		},
		close() {           //关闭新增弹窗
            let index = parent.layer.getFrameIndex(window.name);    //先得到当前iframe层的索引
            parent.layer.close(index);
        }
	},
	created() {
		if(this.tableRowId) this.getUpdateData();
	}
}
</script>